import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class CurlToJava {

    public static Response sendPostRequest(String urlString, Map<String, String> headers, String jsonInputString) {
        Response response = new Response();
        try {
            // URL of the API endpoint
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // Set request method to POST
            connection.setRequestMethod("POST");

            // Set headers
            for (Map.Entry<String, String> header : headers.entrySet()) {
                connection.setRequestProperty(header.getKey(), header.getValue());
            }

            // Allow sending data
            connection.setDoOutput(true);

            // Write JSON data to the request body
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }

            // Get response code and message
            response.setCode(connection.getResponseCode());
            response.setMessage(connection.getResponseMessage());
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null) {
                stringBuilder.append(line.trim());
            }
            in.close();

            String cmdbJson = stringBuilder.toString();
            System.out.println(cmdbJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    public static void main(String[] args) {
        String url = "http://10.142.133.236:9091/sycmdb/1.0.0/8510000/host/search";
        Map<String, String> headers = Map.of(
                "Accept", "application/json, text/plain, */*",
                "Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
                "Auth-Token", "a4f05059-7afd-4a83-a15d-b6246e14b98a",
                "Connection", "keep-alive",
                "Content-Type", "application/json;charset=UTF-8",
                "Cookie", "sessionId=51da4430ada94edc91b0a06cf62; http_scheme=http; tenant=8140000; tenantName=%E5%B1%B1%E8%A5%BF%E7%94%B5%E4%BF%A1%E5%85%AC%E5%8F%B8; auth_token=a4f05059-7afd-4a83-a15d-b6246e14b98a; token=a4f05059-7afd-4a83-a15d-b6246e14b98a; JSESSIONID=851091A2841D42606522C7CC88117DB9; user_info={%22staff_name%22:%22%E9%83%AD%E5%87%AF%22%2C%22tenant_id%22:8140000%2C%22org_id%22:8140000%2C%22staff_code%22:%22ctgcloud1510000%22%2C%22role_code%22:%22tenant%22%2C%22tel%22:%22%22%2C%22isAdmin%22:true%2C%22area_id%22:%228140000%22%2C%22email%22:%22%22%2C%22org_name%22:%22%E5%B1%B1%E8%A5%BF%E7%94%B5%E4%BF%A1%E5%85%AC%E5%8F%B8%22%2C%22job%22:%22%22}",
                "Origin", "http://10.142.133.236:9091",
                "Referer", "http://10.142.133.236:9091/sycmdb/index.html?token=a4f05059-7afd-4a83-a15d-b6246e14b98a",
                "Route", "/system/resource",
                "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0"
        );
        String jsonInputString = "{\"ip\":{\"flag\":\"host_innerip|host_outerip\",\"exact\":1,\"data\":[]},\"asst_list\":[],\"condition\":[{\"biz\":[{\"field\":\"default\",\"operator\":\"$eq\",\"value\":1}]},{\"host\":[]},{\"set\":[]},{\"module\":[]}],\"biz_id\":-1,\"page\":{\"start\":0,\"limit\":10,\"sort\":\"-diff_status,read_status,-modify_date\"}}";

        Response response = sendPostRequest(url, headers, jsonInputString);
        System.out.println("Response Code: " + response.getCode());
        System.out.println("Response Message: " + response.getMessage());
    }
}

class Response {
    private int code;
    private String message;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
